module.exports = function ($scope, $state, $resource, $modal, toaster) {
    $scope.myKeyup = function (e) {
        //IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode == 13) {
            $scope.loadlist();
        }
    };
    $scope.pageInfo = {};
    $scope.pageInfo.pageNo = 1;		//当前页码
    $scope.pageInfo.pageSize = 10;	//每页显示几条
    $scope.pageInfo.totalItems = 0;	//总条数
    //卡列表
    $scope.loadlist = function (company_code) {
        $resource('/api/as/mc/shop/findShopAdminPageList', {}, {}).save($scope.pageInfo, function (res) {
            if (res.errcode === 0) {
                $scope.beanList = res.data.results;
                $scope.pageInfo.totalItems = res.data.totalRecord;
                if (company_code) {
                    let temp_bean = {};
                    for (let index = 0; index < $scope.beanList.length; index++) {
                        const element = $scope.beanList[index];
                        if (element.company_code == company_code) {
                            temp_bean = element;
                            break;
                        }
                    }
                    $scope.info(temp_bean);
                }
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

    $scope.loadlist();

    //详情
    $scope.create = function () {
        var modalInstance = $modal.open({
            template: require('../views/skwxCreateShop.html'),
            controller: 'skwxCreateShop',
            size: 'lg',
            resolve: {
            }
        });
        //关闭模态框刷新页面
        modalInstance.result.then(function (result) {
            $scope.loadlist(result);
        });
    }

    //详情
    $scope.info = function (bean) {
        var modalInstance = $modal.open({
            template: require('../views/skwxShopInfo.html'),
            controller: 'skwxShopInfo',
            size: 'lg',
            resolve: {
                bean: function () {
                    return bean;
                }
            }
        });
        //关闭模态框刷新页面
        modalInstance.result.then(function () {
            $scope.loadlist();
        });
    }

    //封杀
    $scope.stop = function (bean) {
        if (confirm('确认封杀此店铺吗？') == true) {
            let para = {
                id: bean.id,
                company_code: bean.company_code,
                state: '0'
            }
            $resource('/api/as/mc/shop/updateShopAdminState', {}, {}).save(para, function (res) {
                if (res.errcode === 0) {
                    toaster.success({ title: '', body: '恭喜您，封杀成功！' });
                    $scope.loadlist();
                } else {
                    toaster.error({ title: '', body: res.errmsg });
                }
            });
        }
    }

    //解封
    $scope.start = function (bean) {
        if (confirm('确认解封此店铺吗？') == true) {
            let para = {
                id: bean.id,
                company_code: bean.company_code,
                state: '1'
            }
            $resource('/api/as/mc/shop/updateShopAdminState', {}, {}).save(para, function (res) {
                if (res.errcode === 0) {
                    toaster.success({ title: '', body: '恭喜您，解封成功！' });
                    $scope.loadlist();
                } else {
                    toaster.error({ title: '', body: res.errmsg });
                }
            });
        }
    }

    //删除卡
    $scope.delete = function (bean) {
        if (confirm('确认删除此条记录吗？') == true) {
            $resource('/api/as/mc/shopclass/delete', {}, {}).save(bean, function (res) {
                if (res.errcode === 0) {
                    $scope.loadlist();
                } else {
                    toaster.error({ title: '', body: res.errmsg });
                }
            });
        }
    }

};